﻿using System;
using System.Collections.Generic;

namespace _30._2_面向对象_索引器
{
    class Program
    {
        static void Main(string[] args)
        {
            // 生成 dog 对象
            var dog = new Dog(6, "小黑", 180);
            var son1 = new Dog(1, "小祖宗", 150);
            var son2 = new Dog(1, "小祖宗", 100);

            int age = 1;
            string name = "小祖宗";


            //将 son1 录入到 dog 的户口本
            dog[age, name] = son1;

            Console.WriteLine($"Before  {dog[age, name]}");

            // 发现抱错孩子，交换
            dog[age, name] = son2;

            // 证明真的交换了吗？
            Console.WriteLine($"After  {dog[age, name]}");

            S s =new S{age=5,name="dd" };
            Console.WriteLine( s );
            S ss = new S();
            Console.WriteLine( ss );

            S sss = new S(6,"小六");
            Console.WriteLine( sss);
            
            S ssss = default;
            Console.WriteLine( ssss);

            ssss.age = 10;
            ssss.name = "小十";
            Console.WriteLine( ssss);
        }
        struct S
        {
            public int age;
            public string name;
            public S(int age,string name) 
            {
                this.age = age;
                this.name = name;
            }
            public override string ToString()
            {
                return $"name = {name} , age = {age}";
            }
        }
    }

    #region  索引的重载
    class Person
    {
        private string[] arr;
        public virtual string this[int index]
        {
            get => arr[index];
            set => arr[index] = value;
        }
    }

    class Student : Person
    {
        private string[] arr2;

        public override string this[int index]
        {
            get => arr2[index] + base[index];
            set => arr2[index] = value;
        }
    }
    #endregion

    #region 多参数索引器
    class SurperMan
    {
        private int[,] arr;
        public SurperMan(int x, int y)
        {
            arr = new int[x, y];
        }

        // 在本例，x、y 是索引值
        public int this[int x, int y]
        {
            get
            {
                // todo : x 钳制，y 钳制
                var data = arr[x, y];

                return data;
            }
        }
    }


    class Dog
    {
        public int age;
        public string name;
        public int size;
        List<Dog> family;

        public Dog(int age, string name, int size)
        {
            this.age = age;
            this.name = name;
            this.size = size;
            family = new List<Dog>();
        }

        public Dog this[int age, string name]
        {
            get
            {
                // 查询断言成立的 Dog 
                var dog = family.Find(v => v.age == age && v.name == name);
                return dog;
            }
            set
            {
                // 查询断言成立的  索引值
                var index = family.FindIndex(v => v.age == age && v.name == name);
                if (index == -1)
                {
                    family.Add(value);
                }
                else
                {
                    family[index] = value;
                }
            }
        }
        public override string ToString()
        {
            return size.ToString();
        }

    }

    #endregion

}
